package com.lzy.huijinjiazu.mvp.ui.fragment;

import android.os.Bundle;
import android.os.Message;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import com.jess.arms.base.BaseFragment;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.lzy.huijinjiazu.R;
import com.lzy.huijinjiazu.app.EventBusTags;
import com.lzy.huijinjiazu.di.component.DaggerShoppingCartListComponent;
import com.lzy.huijinjiazu.di.module.ShoppingCartListModule;
import com.lzy.huijinjiazu.mvp.contract.ShoppingCartListContract;
import com.lzy.huijinjiazu.mvp.model.entity.ShoppingCart;
import com.lzy.huijinjiazu.mvp.presenter.ShoppingCartListPresenter;
import com.lzy.huijinjiazu.mvp.ui.activity.ConfirmOrderActivity;
import com.lzy.huijinjiazu.mvp.ui.adapter.ShoppingCartAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import org.simple.eventbus.Subscriber;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;


public class ShoppingCartListFragment extends BaseFragment<ShoppingCartListPresenter> implements ShoppingCartListContract.View {


    @BindView(R.id.rv_list)
    RecyclerView mRvList;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mRefreshLayout;
    @BindView(R.id.tv_commit)
    TextView mTvCommit;
    @BindView(R.id.cb_all)
    CheckBox mCbAll;
    private ShoppingCartAdapter mAdapter;
    private int mI;

    public static ShoppingCartListFragment newInstance(String type, int i) {
        ShoppingCartListFragment fragment = new ShoppingCartListFragment();
        Bundle bundle = new Bundle();
        bundle.putString("type", type);
        bundle.putInt("i", i);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void setupFragmentComponent(AppComponent appComponent) {
        DaggerShoppingCartListComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .shoppingCartListModule(new ShoppingCartListModule(this))
                .build()
                .inject(this);
    }

    @Override
    public View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_shopping_cart_list, container, false);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        mI = getArguments().getInt("i");
        mTvCommit.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.colorAccent));
        initRecyclerView();
        listener();
        initData();
    }

    private void initRecyclerView() {
        ArmsUtils.configRecyclerView(mRvList, new LinearLayoutManager(getContext()));
        mAdapter = new ShoppingCartAdapter();
        mRvList.setAdapter(mAdapter);

    }

    private void listener() {
        mAdapter.setOnCheckClickListener((isCheck, view) -> {
            itemCheck();
        });
    }

    private void initData() {
        List<ShoppingCart> data = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            data.add(new ShoppingCart());
        }
        mAdapter.setNewData(data);
    }

    @OnClick({R.id.tv_commit, R.id.cb_all})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_commit:
                ArmsUtils.startActivity(ConfirmOrderActivity.class);
                break;
            case R.id.cb_all:
                changData(mCbAll.isChecked());
                break;
        }
    }

    @Subscriber(tag = EventBusTags.SHOPPING_STATE)
    public void shoppingEditEvent(Message message) {
        if (message.what == mI) {
            changeEditData((Boolean) message.obj);
        }
    }

    private void changeEditData(boolean isEdit) {
        List<ShoppingCart> data = mAdapter.getData();
        for (ShoppingCart datum : data) {
            datum.setEdit(isEdit);
        }
        mAdapter.notifyDataSetChanged();
    }

    private void changData(boolean isCheck) {
        List<ShoppingCart> data = mAdapter.getData();
        for (ShoppingCart datum : data) {
            datum.setCheck(isCheck);
        }
        mAdapter.notifyDataSetChanged();
        mCbAll.setChecked(isCheck);
    }

    private void itemCheck() {
        List<ShoppingCart> data = mAdapter.getData();
        for (ShoppingCart datum : data) {
            if (!datum.isCheck()) {
                mCbAll.setChecked(false);
                return;
            }
        }
        mCbAll.setChecked(true);
    }
}
